## 第二版
class Node:
    def __init__(self, value, next=None, prev=None):
        self.value = value
        self.next = next
        self.prev = prev
    
    def __repr__(self):
        return "{} <-- {} --> {}".format(
            self.prev.value if self.prev else None, 
            self.value, 
            self.next.value if self.next else None
        )

    
class LinkList:
    '''还可以在完善下'''
    __slots__ = 'head tail length'.split()
    def __init__(self):
        self.head = None
        self.tail = None
        self.length = 0
        
    def insert(self, index, value):
        '''找个位置插入'''
        newnode = Node(value)
        flag = False
        for i, current in enumerate(self.iterlink()):
            if index <= 0: # 头插入      
                self.head = newnode   
                flag = True
            elif i == index: # 中间插入
                prev = current.prev
                prev.next = newnode
                newnode.prev = prev
                flag = True
            if flag:
                newnode.next = current
                current.prev = newnode
                self.length += 1
                break
        else:
            self.append(value)       
        
    def append(self, value):
        '''尾部追加'''
        node = Node(value)
        if self.head is None:           
            self.head = node         
        else:
            self.tail.next = node
            node.prev = self.tail
        self.tail = node
        self.length += 1
        return self
    
    def pop(self):
        '''尾部删除'''
        if not self.head:
            raise Exception('Empty')
        current = self.tail
        prev = self.tail.prev
        self.tail = prev
        self.tail.next = None
        self.length -= 1
        return current.value
   
    def remove(self, index):
        '''移除某个位置的'''
        if index < 0:
            raise IndexError('error index')
        elif self.head is self.tail:
            raise Exception('Empty')
        for i, current in enumerate(self.iterlink()):
            if index == i: # 找到索引
                prev = current.prev
                next = current.next
                if current.prev is current.next: # 只有一个元素时
                    self.pop()
                elif current.prev is None: # 在首部删除
                    next.prev = None
                    self.head = next
                    self.length -= 1
                elif current.next is None: # 在尾部删除
                    self.pop()
                else: # 在中间删除                  
                    prev.next = next
                    next.prev = prev
                    self.length -= 1
                break
        else:
            raise IndexError('out ')
    
    def iterlink(self):
        '''支持一个遍历的方法'''
        current = self.head
        while current:
            yield current
            current = current.next
    
    def __len__(self):
        '''可以求得长度'''
        return self.length
    
    def __iter__(self):
        '''实例本身就可以迭代，使用更方便'''
        return self.iterlink()
    
    def __setitem__(self, index, value):
        '''可以直接用中括号的方式进行赋值'''
        self.insert(index, value)
    
    def __getitem__(self, index):
        '''支持中括号式访问'''
        # raise Index Error
        for i, item in enumerate(self):
            if index == i:
                return item.value
ll = LinkList()
ll.append(1).append(2).append(3)
for i in ll:
    print(i)